package jmu.edu.yjc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jmu.edu.yjc.Utils.BeanCopyUtil;
import jmu.edu.yjc.Utils.WebUtils;
import jmu.edu.yjc.config.CustomCellWriteHeightConfig;
import jmu.edu.yjc.config.CustomCellWriteWeightConfig;
import jmu.edu.yjc.constants.SystemConstants;
import jmu.edu.yjc.domain.ResponseResult;
import jmu.edu.yjc.domain.entity.Category;
import jmu.edu.yjc.domain.entity.Elderly;
import jmu.edu.yjc.domain.entity.User;
import jmu.edu.yjc.domain.vo.PageVo;
import jmu.edu.yjc.domain.vo.category.ExcelCategoryVo;
import jmu.edu.yjc.domain.vo.common.TypeVo;
import jmu.edu.yjc.domain.vo.elderly.ElderlyVo;
import jmu.edu.yjc.domain.vo.elderly.ExcelElderlyVo;
import jmu.edu.yjc.domain.vo.user.UserVo;
import jmu.edu.yjc.enums.AppHttpCodeEnum;
import jmu.edu.yjc.exception.SystemException;
import jmu.edu.yjc.interfaces.SaveInterface;
import jmu.edu.yjc.listener.MyExcelListener;
import jmu.edu.yjc.mapper.ElderlyMapper;
import jmu.edu.yjc.service.ElderlyService;
import jmu.edu.yjc.service.user.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Elderly)表服务实现类
 *
 * @author makejava
 * @since 2023-02-05 22:42:00
 */
@Service("elderlyService")
public class ElderlyServiceImpl extends ServiceImpl<ElderlyMapper, Elderly> implements ElderlyService, SaveInterface<ExcelElderlyVo> {
    @Resource
    private ExportService exportService;
    @Resource
    private UserService userService;
    @Override
    public ResponseResult getElderlyList(Integer pageNum, Integer pageSize, String name, String phoneNumber) {
        LambdaQueryWrapper<Elderly> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name),Elderly::getName,name);
        queryWrapper.like(StringUtils.hasText(phoneNumber),Elderly::getPhoneNumber,phoneNumber);
        Page<Elderly> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        return ResponseResult.okResult(new PageVo(page.getRecords(),page.getTotal()));
    }

    @Override
    public void export(HttpServletResponse response) {
        //获取需要导出的数据
        List<Elderly> elderlies = list();
        List<ExcelElderlyVo> excelElderlyVos = BeanCopyUtil.copyBeanList(elderlies, ExcelElderlyVo.class);
        exportService.export(response, "老人.xlsx", excelElderlyVos, ExcelElderlyVo.class);
    }

    @Override
    public ResponseResult uploadExcel(MultipartFile file) {
        try {
            //headRowNumber 2 代表从第3行开始导入 第一行为大标题 第二行为表头
            EasyExcel.read(file.getInputStream(),ExcelElderlyVo.class,
                    new MyExcelListener(this)).headRowNumber(1).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(-1,"上传失败");
        }
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getElderly(String name) {
        LambdaQueryWrapper<Elderly> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Elderly::getName,name);
        return ResponseResult.okResult(list(wrapper));
    }

    @Override
    public Elderly getByIdentity(String identityId) {
        LambdaQueryWrapper<Elderly> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Elderly::getIdentityId,identityId);
        return getOne(wrapper);
    }

    @Override
    public ElderlyVo queryByIdentityId(String identityId) {
        LambdaQueryWrapper<Elderly> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Elderly::getIdentityId,identityId);
        Elderly elderly = getOne(wrapper);
        ElderlyVo elderlyVo = BeanCopyUtil.copyBeans(elderly,ElderlyVo.class);
        User user = userService.getById(elderly.getUserId());
        UserVo userVo = BeanCopyUtil.copyBeans(user,UserVo.class);
        elderlyVo.setUser(userVo);
        return elderlyVo;
    }

    @Override
    public List<TypeVo> getAgeType() {
        return baseMapper.getAgeType();
    }

    @Transactional
    @Override
    public void save(List<ExcelElderlyVo> list, Map<String, Object> param) {
        List<Elderly> elderlies = BeanCopyUtil.copyBeanList(list, Elderly.class);
        for (Elderly elderly : elderlies) {
            if("男".equals(elderly.getSex())){
                elderly.setSex(SystemConstants.MAN);
            }else if("女".equals(elderly.getSex())){
                elderly.setSex(SystemConstants.WOMAN);
            }else {
                elderly.setSex("-1");
            }
            LambdaUpdateWrapper<Elderly> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(Elderly::getIdentityId,elderly.getIdentityId())
                    .eq(Elderly::getName,elderly.getName());
            saveOrUpdate(elderly,wrapper);
        }
    }
}
